layui.use(['element', 'table', 'laytpl'], function () {
    var $ = layui.$,
        table = layui.table,
        layer = layui.layer;

    //禁止当前页的浏览器默认右点击事件
    document.oncontextmenu = function () {
        return false;
    };

    //初始化菜单
    var $menu = $("#menu");
    $menu.menu();

    //获取楼和楼层加载房间
    $(function () {
        var floorNum = $(".active.floorBtn").attr("data-floor");
        var bildNo = $(".active.buildBtn").data("bid");
        if (floorNum == undefined) {
            floorNum = 0;
        }
        var params = "buildingId=" + bildNo + "&floor=" + floorNum;
        selectRoomListByBuildFloor(params);
    });

    //点击楼宇获取相关数据
    $("table").on('click', '.buildBtn', function () {
        //点击楼宇后先加载该楼宇的楼层
        var bildNo = $(this).attr('data-bid');
        var param = "buildingId=" + bildNo;
        getFloorsByBid(param);
        //加载完楼层后获取选中的楼宇和选中的楼层
        var floorNum = $(".active.floorBtn").data("floor");
        if (floorNum == undefined) {
            floorNum = 0;
        }
        var params = "buildingId=" + bildNo + "&floor=" + floorNum;
        $(".buildBtn ").addClass("layui-btn-primary").removeClass("active");
        $(this).removeClass("layui-btn-primary").addClass("active");
        //楼宇根据bid查询床位数据
        statisticsBuid(params);
        //楼宇根据bid和楼层floor查询床位数据
        statisticsFloor(params);
        //根据楼宇,楼层查询房间
        selectRoomListByBuildFloor(params);
        $("#elderlyName").val("");
        $("#elderlyId").val("");
    });

    //点击楼层获取相关数据
    $("table ").on('click', '.floorBtn', function () {
        var floorNum = $(this).attr("data-floor");
        var bildNo = $(".active.buildBtn").data("bid");
        $(".floorBtn ").addClass("layui-btn-primary").removeClass("active");
        $(this).removeClass("layui-btn-primary").addClass("active");
        var params = "buildingId=" + bildNo + "&floor=" + floorNum;
        //楼宇根据bid和楼层floor查询床位数据
        statisticsFloor(params);
        //根据楼宇,楼层查询房间
        selectRoomListByBuildFloor(params);
        $("#elderlyName").val("");
        $("#elderlyId").val("");
    });

    //根据楼宇查询楼层
    var getFloorsByBid = function getFloorsByBid(param) {
        $.ajax({
            url: "/gbElderly/getFloorsByBid",
            type: 'post',
            dataType: 'json',
            data: param,
            async: false,
            success: function (date) {
                if (date.code == 200) {
                    var floorList = date.data.floorList;
                    var $td = $("#floorList").find("td");
                    $td.find("a").remove();
                    var tdHtmlNul = '<a href="/room" class="layui-btn layui-btn-danger">创建房间</a> ';
                    if (floorList == 0) {
                        $td.append(tdHtmlNul);
                        return false;
                    } else {
                        $.each(floorList, function (i, d) {
                            var $a = $('<a href="javascript:void(0)" data-floor="' + d + '">' + d + '层</a>');
                            $td.append($a);
                            if (i == 0) {
                                $a.addClass('layui-btn floorBtn active');
                            } else {
                                $a.addClass('layui-btn floorBtn layui-btn-primary');
                            }
                        });
                    }
                }
            },
            error: function (e) {
                console.log("加载失败");
            }
        });
    }

//根据楼宇查询床位统计数据
    var statisticsBuid = function statisticsBuid(params) {
        $.ajax({
            url: "/gbElderly/statisticsBuid",
            type: 'post',
            dataType: 'json',
            data: params,
            async: false,
            success: function (date) {
                if (date.code == 200) {
                    var bedsBuild = date.data.bedsBuild;
                    $.each(bedsBuild, function (i, d) {
                        if (i == 0) {
                            $("#buidBedNum0").text(d.floor);
                        } else if (i == 1) {
                            $("#buidBedNum1").text(d.floor);
                        } else if (i == 2) {
                            $("#buidBedNum2").text(d.floor);
                        }
                    });
                }
            },
            error: function (e) {
                console.log("加载失败");
            }
        });
    }

    //根据楼宇楼层查询床位统计数据
    var statisticsFloor = function statisticsFloor(params) {
        $.ajax({
            url: "/gbElderly/statisticsFloor",
            type: 'post',
            dataType: 'json',
            data: params,
            async: false,
            success: function (date) {
                if (date.code == 200) {
                    var bedsFloor = date.data.bedsFloor;
                    $.each(bedsFloor, function (i, d) {
                        if (i == 0) {
                            $("#floorBedNum0").text(d.floor);
                        } else if (i == 1) {
                            $("#floorBedNum1").text(d.floor);
                        } else if (i == 2) {
                            $("#floorBedNum2").text(d.floor);
                        }
                    });
                }
            },
            error: function (e) {
                console.log("加载失败");
            }
        });
    }

    //根据楼宇,楼层查询房间
    function selectRoomListByBuildFloor(params) {
        $.ajax({
            url: "/gbElderly/selectRoomListByBuildFloor",
            type: 'post',
            dataType: 'json',
            data: params,
            async: false,
            success: function (date) {
                if (date.code == 200) {
                    var roomList = date.data.roomList;
                    var $div = $("#roomList").find(".room-list");
                    var $divrlist = $("#roomList").find(".room-item");
                    $divrlist.remove();
                    var divHtmlNul =
                        '                            <div class="room-item">\n' +
                        '                                <div class=" room-box">\n' +
                        '                                    <div class="bed-box" style="margin: auto;">\n' +
                        '                                        <div class="bed-info">\n' +
                        '                                            <div></div>\n' +
                        '                                            <div class="right"></div>\n' +
                        '                                        </div>\n' +
                        '                                        <div class="elderly-box">\n' +
                        '                                            <div class="avatar-box">\n' +
                        '                                                <img src="" alt="">\n' +
                        '                                            </div>\n' +
                        '                                            <div class="elderly-info">\n' +
                        '                                                <div class="one" style="font-size: 18px;margin-left: -45px;">未创建房间</div>\n' +
                        '                                                <div></div>\n' +
                        '                                            </div>\n' +
                        '                                        </div>\n' +
                        '                                    </div>\n' +
                        '                                </div>\n' +
                        '                        </div> ';
                    if (roomList == 0) {
                        $div.append(divHtmlNul);
                        return false;
                    } else {//根据房间id查询床位
                        $.each(roomList, function (i, d) {
                            var $cw = '';
                            $.ajax({
                                url: "/gbElderly/getBedsByRid",
                                type: 'post',
                                dataType: 'json',
                                data: "roomId=" + d.id,
                                async: false,
                                success: function (date) {
                                    if (date.code == 200) {
                                        var data = date.data;
                                        if (data != null && data.length > 0) {//有床位
                                            $.each(data, function (i, d) {
                                                var cwtype = '';
                                                var rzstatus = '';
                                                var ename = '';
                                                var hljb = '';
                                                var avatar = "";
                                                if (d.type == 1) {//普通床位
                                                    cwtype = '<div>' + d.bcode + '(普通床位)</div>\n';
                                                } else if (d.type == 2) {//护理床位
                                                    cwtype = '<div>' + d.bcode + '(护理床位)</div>\n';
                                                }else if (d.typeName != null) {
                                                    cwtype = '<div>' + d.bcode + '('+d.typeName+')</div>\n';
                                                }
                                                if (d.bstatus == 1) {//空闲
                                                    rzstatus = '<div class="right">空闲</div>\n';
                                                    ename = '<div class="one"><a href=/gbElderly/ruzhu?id=' + d.bid + ' style="background-color: #2cad50" class="layui-btn ruzhu" data-rzbid="' + d.bid + '">入住</a></div>\n';
                                                    hljb = '<div></div>\n'
                                                    $cw += '  <div class="bed-box" style="background-color: #adf7c2">\n' +
                                                        '                                        <div class="bed-info">\n' +
                                                        cwtype +
                                                        rzstatus +
                                                        '                                        </div>\n' +
                                                        '                                        <div class="elderly-box">\n' +
                                                        '                                            <div class="avatar-box">\n' +
                                                        '                                                <img src="/static/imgs/bed.png" alt="">\n' +
                                                        '                                            </div>\n' +
                                                        '                                            <div class="elderly-info">\n' +
                                                        ename +
                                                        hljb +
                                                        '                                            </div>\n' +
                                                        '                                        </div>\n' +
                                                        '                                    </div>\n';
                                                } else if (d.bstatus == 2) {//已入住
                                                    rzstatus = '<div class="right">已入住</div>\n';
                                                    ename = '<div class="one">' + d.ename + '</div>\n';
                                                    hljb = '<div>' + d.mealName + '</div>\n'
                                                    avatar = '<img src="/static/imgs/elder.png" alt="">\n';
                                                    if (d.avatar != "") {
                                                        avatar = '<img src="/storage/files/' + d.avatar + '" alt="">\n';
                                                    } else {
                                                        avatar = '<img src="/static/imgs/elder.png" alt="">\n';
                                                    }
                                                    $cw +=
                                                        '<div class="bed-box menu1"  data-id="' + d.eid + '" style="background-color: #e6d19a">\n' +
                                                        '                              <div class="bed-info">\n' +
                                                        cwtype +
                                                        rzstatus +
                                                        '                                        </div>\n' +
                                                        '                                        <div class="elderly-box">\n' +
                                                        '                                            <div class="avatar-box">\n' +
                                                        avatar +
                                                        '                                            </div>\n' +
                                                        '                                            <div class="elderly-info">\n' +
                                                        ename +
                                                        hljb +
                                                        '                                            </div>\n' +
                                                        '                                        </div>\n' +
                                                        '</div>\n';

                                                }
                                            });
                                        } else {//未创建床位
                                            $cw = '  <div class="one"><a href="/bed" class="layui-btn layui-btn-danger" style="margin-left: 250px;margin-top: 160px;">创建床位</a></div>\n';
                                        }
                                        var $d = $(
                                            '                            <div class="room-item">\n' +
                                            '                                <div class="room-code">' + d.buildingName + '-' + d.code + '号房' + '</div>\n' +
                                            '                                <div class=" room-box">\n' +
                                            $cw +
                                            '                                </div>\n' +
                                            '                        </div>'
                                        );
                                        $div.append($d);
                                    }
                                },
                                error: function (e) {
                                    console.log("加载失败");
                                }
                            });
                        });
                        $(".menu1").mousedown(function (event) {
                            if (event.which == 3) {
                                var id = $(this).data("id");
                                $(".message").data("id", id);
                                $(".record").data("id", id);
                                $(".service").data("id", id);
                                $(".checkInUpdate").data("id", id);
                                $(".checkOut").data("id", id);
                                // 鼠标点击位置坐标
                                var mouseX = event.pageX;
                                var mouseY = event.pageY;
                                // ul菜单出现
                                $menu.css({
                                    "left": mouseX - 10,
                                    "top": mouseY - 10
                                }).show();

                            }
                        })
                    }
                }
            },
            error: function (e) {
                console.log("加载失败");
            }
        });
    }

    //当鼠标移出菜单
    $menu.mouseleave(function () {
        $("#menu").hide()
    });

    //点击退院
    $(".checkOut").on('click', function () {
        $("#menu").hide();
        var id = $(this).data("id");
        ww= layer.open({
            type: 2,
            content: '/checkoutrecords/checkOut?elderlyId=' + id,
            area: ['920px', '700px'],
            title: '信息'
        });
    });

    //点击入住变更
    $(".checkInUpdate").on('click', function () {
        $("#menu").hide();
        var id = $(this).data("id");
       ee= layer.open({
            type: 2,
            content: '/gbElderly/checkInUpdate?elderlyId=' + id,
            area: ['920px', '600px'],
            title: '信息'
        });
    });
    //点击基本信息
    $(".message").on('click', function () {
        $("#menu").hide();
        var id = $(this).data("id");
        layer.open({
            type: 2,
            content: '/gbElderly/messageUI?elderlyId=' + id + '&type=1',
            area: ['700px', '550px'],
            title: '信息'
        });
    });

    //点击健康数据
    $(".record").on('click', function () {
        $("#menu").hide();
        var id = $(this).data("id");
        layer.open({
            type: 2,
            content: '/gbElderly/messageUI?elderlyId=' + id + '&type=2',
            area: ['700px', '550px'],
            title: '信息'
        });

    });

    //点击服务记录
    $(".service").on('click', function () {
        $("#menu").hide();
        var id = $(this).data("id");
        layer.open({
            type: 2,
            content: '/gbElderly/messageUI?elderlyId=' + id + '&type=3',
            area: ['700px', '550px'],
            title: '信息'
        });
    });

    //选择老人
    $("#elderlyName").on('click', function () {
        layer.open({
            type: 2,
            content: '/gbassessRecords/select/elderly',
            area: ['800px', '600px'],
            title: '选择老人',
            btn: ['确定 '],
            yes: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                var checkStatus = iframeWin.layui.table.checkStatus('mytable');
                if (checkStatus.data.length < 1 || checkStatus.data.length > 1) {
                    iframeWin.layui.layer.msg("请选择一位老人");
                    return false;
                }
                var name = checkStatus.data[0].name;
                var id = checkStatus.data[0].id;
                $("#elderlyName").val(name);
                $("#elderlyId").val(id);
                layer.close(index);
                selectBedByElderId();
            }
        });
    });

    //根据老人姓名查询入住信息
    function selectBedByElderId() {
        $.ajax({
            url: "/gbElderly/selectBedByElderId",
            type: 'post',
            dataType: 'json',
            data: {elderlyId: $("#elderlyId").val()},
            success: function (msg) {
                if (msg.code == 200) {
                    var data = msg.data.bedModeo;
                    if (data == null) {
                        layer.msg($("#elderlyName").val() + "未入住", {time: 1500});
                    } else {//老人已入住
                        $("#buids").find("a").each(function () {//选中入住的楼宇
                            $(this).addClass("layui-btn-primary").removeClass("active");
                            if ($(this).data("bid") == data.buildingId) {
                                $(this).removeClass("layui-btn-primary").addClass("active");
                            }
                        });
                        $("#floors").find("a").each(function () {//选中入住的楼层
                            $(this).addClass("layui-btn-primary").removeClass("active");
                            if ($(this).data("floor") == data.floor) {
                                $(this).removeClass("layui-btn-primary").addClass("active");
                            }
                        });
                        var params = "buildingId=" + data.buildingId + "&floor=" + data.floor;
                        //楼宇根据bid查询床位数据
                        statisticsBuid(params);
                        //楼宇根据bid和楼层floor查询床位数据
                        statisticsFloor(params);
                        var $div = $("#roomList").find(".room-list");
                        var $divrlist = $("#roomList").find(".room-item");
                        $divrlist.remove();
                        var $cw = '';
                        $.ajax({
                            url: "/gbElderly/getBedsByRid",
                            type: 'post',
                            dataType: 'json',
                            data: "roomId=" + data.roomId,
                            success: function (date) {
                                if (date.code == 200) {
                                    var da = date.data;
                                    $.each(da, function (i, d) {
                                        var cwtype = '';
                                        var rzstatus = '';
                                        var ename = '';
                                        var hljb = '';
                                        var avatar = "";
                                        if (d.type == 1) {//普通床位
                                            cwtype = '<div>' + d.bcode + '(普通床位)</div>\n';
                                        } else if (d.type == 2) {//护理床位
                                            cwtype = '<div>' + d.bcode + '(护理床位)</div>\n';
                                        }else if (d.typeName != null) {
                                            cwtype = '<div>' + d.bcode + '('+d.typeName+')</div>\n';
                                        }
                                        if (d.bstatus == 1) {//空闲
                                            rzstatus = '<div class="right">空闲</div>\n';
                                            ename = '<div class="one"><a href=/gbElderly/ruzhu?id=' + d.bid + ' style="background-color: #2cad50" class="layui-btn ruzhu" data-rzbid="' + d.bid + '">入住</a></div>\n';
                                            hljb = '<div></div>\n'
                                            $cw += '  <div class="bed-box" style="background-color: #adf7c2">\n' +
                                                '                                        <div class="bed-info">\n' +
                                                cwtype +
                                                rzstatus +
                                                '                                        </div>\n' +
                                                '                                        <div class="elderly-box">\n' +
                                                '                                            <div class="avatar-box">\n' +
                                                '                                                <img src="/static/imgs/bed.png" alt="">\n' +
                                                '                                            </div>\n' +
                                                '                                            <div class="elderly-info">\n' +
                                                ename +
                                                hljb +
                                                '                                            </div>\n' +
                                                '                                        </div>\n' +
                                                '                                    </div>\n';
                                        } else if (d.bstatus == 2) {//已入住
                                            rzstatus = '<div class="right">已入住</div>\n';
                                            ename = '<div class="one">' + d.ename + '</div>\n';
                                            hljb = '<div>' + d.mealName + '</div>\n'
                                            avatar = '<img src="/static/imgs/elder.png" alt="">\n';
                                            if (d.avatar != "") {
                                                avatar = '<img src="/storage/files/' + d.avatar + '" alt="">\n';
                                            } else {
                                                avatar = '<img src="/static/imgs/elder.png" alt="">\n';
                                            }
                                            var backColor = "";
                                            if (data.id == d.bid) {
                                                backColor = "#428bd6";
                                            } else {
                                                backColor = "#e6d19a";
                                            }
                                            $cw += '  <div class="bed-box" style="background-color: ' + backColor + '">\n' +
                                                '                                        <div class="bed-info">\n' +
                                                cwtype +
                                                rzstatus +
                                                '                                        </div>\n' +
                                                '                                        <div class="elderly-box">\n' +
                                                '                                            <div class="avatar-box">\n' +
                                                avatar +
                                                '                                            </div>\n' +
                                                '                                            <div class="elderly-info">\n' +
                                                ename +
                                                hljb +
                                                '                                            </div>\n' +
                                                '                                        </div>\n' +
                                                '                                    </div>\n';
                                        }
                                    });
                                    var $d = $(
                                        '                            <div class="room-item">\n' +
                                        '                                <div class="room-code">' + data.buildingName + '-' + data.roomName + '号房' + '</div>\n' +
                                        '                                <div class=" room-box">\n' +
                                        $cw +
                                        '                                </div>\n' +
                                        '                        </div>'
                                    );
                                    $div.append($d);
                                }
                            },
                            error: function (e) {
                                console.log("加载失败");
                            }
                        });
                    }
                }
            },
            error: function (e) {
                console.log("加载失败");
            }
        });
    }

});